package com.amsp.oa.oademo.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @Author 张代浩
 */
@Slf4j
public class oConvertUtils {

    public static String getString(String s) {
        return (getString(s, ""));
    }

    public static String getString(String s, String defval) {
        if (isEmpty(s)) {
            return (defval);
        }
        return (s.trim());
    }

    public static boolean isEmpty(Object object) {
        if (object == null) {
            return (true);
        }
        if ("".equals(object)) {
            return (true);
        }
        if (CommonConstant.STRING_NULL.equals(object)) {
            return (true);
        }
        return (false);
    }

    /**
     * map转str
     *
     * @param map
     * @return
     */
    public static String mapToString(Map<String, String[]> map) {
        if (map == null || map.size() == 0) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String[]> entry : map.entrySet()) {
            String key = entry.getKey();
            String[] values = entry.getValue();
            sb.append(key).append("=");
            sb.append(values != null ? StringUtils.join(values, ",") : "");
            sb.append("&");
        }

        String result = sb.toString();
        if (result.endsWith("&")) {
            result = result.substring(0, sb.length() - 1);
        }
        return result;
    }

}
